"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : binary数据类型
Case Name   : COPY FROM一个文件拷贝数据到一个有binary类型的表
Create At   : 2022.08.11
@zou_jialiang0501129488
Description :
    1、创建b库
    2、b库下查询dolphin插件
    3、建表并插入数据后执行copy数据至文件-清空表-从文件中copy至表
    4、清理环境
Expect      :
    1、成功
    2、成功
    3、成功
    4、成功
History     :
"""

import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro


class DatatypeTest01(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.common = Common()
        self.sh_primary = CommonSH('PrimaryDbUser')
        self.user_node = Node('PrimaryDbUser')
        self.constant = Constant()
        self.copy_path = os.path.join(macro.DB_INSTANCE_PATH,
                                      'Binary_Case0041.txt')
        self.db_name = 'binary_case41'

    def test_binary(self):
        text = '-----step1:创建兼容b库数据库;expect:成功-----'
        self.log.info(text)
        sql_cmd = f"drop database if exists {self.db_name};" \
                  f"create database {self.db_name} " \
                  f"dbcompatibility ='B' encoding ='UTF8';"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd, dbname='postgres')
        self.log.info(sql_res)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, sql_res,
                      '执行失败' + text)

        text = '-----step2:b库下查询dolphin插件;expect:自动加载成功,无需手动创建-----'
        self.log.info(text)
        sql_cmd = "select extname from pg_extension where extname ='dolphin';"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd, dbname=self.db_name)
        self.log.info(sql_res)
        self.assertIn('extname', sql_res, '查询失败')
        self.assertIn('dolphin', sql_res, '执行失败' + text)

        text = '-----step3:建表并插入数据后执行copy数据至文件-清空表-从文件中copy至表;' \
               'expect:成功-----'
        self.log.info(text)
        cmd = f"rm -rf {self.copy_path};touch {self.copy_path};" \
              f"ls {macro.DB_INSTANCE_PATH} | grep 'Binary_Case0041.txt';"
        self.log.info(cmd)
        res = self.common.get_sh_result(self.user_node, cmd)
        self.assertIn('Binary_Case0041.txt', res, '文件创建失败')

        self.log.info('-----建表插数据-----')
        sql = f"drop table if exists t_binary_041;" \
              f"create table t_binary_041(field_name binary(6), id int);" \
              f"insert into t_binary_041 values('10101', 1);" \
              f"insert into t_binary_041 values ('1001', 6);" \
              f"insert into t_binary_041 values ('101', 6);" \
              f"insert into t_binary_041 values  ('10001', 6);"
        self.log.info(sql)
        res = self.sh_primary.execut_db_sql(sql, dbname=self.db_name)
        self.assertIn('CREATE TABLE', res, '建表失败')
        self.assertEqual(res.count('INSERT 0 1'), 4, '插入数据失败')

        sql_select = f"select * from t_binary_041;"
        self.log.info(sql_select)
        check_msg = self.sh_primary.execut_db_sql(sql_select,
                                                  dbname=self.db_name)
        self.log.info(check_msg)

        self.log.info('-----copy表数据至文件-----')
        sql = f"copy t_binary_041 to '{self.copy_path}';"
        self.log.info(sql)
        res = self.sh_primary.execut_db_sql(sql, dbname=self.db_name)
        self.log.info(res)
        self.assertIn('COPY 4', res, '建表失败')

        self.log.info('-----truncate表-----')
        sql = f"truncate table t_binary_041;"
        self.log.info(sql)
        res = self.sh_primary.execut_db_sql(sql, dbname=self.db_name)
        self.log.info(res)
        self.assertIn('TRUNCATE TABLE', res, '建表失败')

        self.log.info('-----copy文件数据至表-----')
        sql = f"copy t_binary_041 from '{self.copy_path}';"
        self.log.info(sql)
        res = self.sh_primary.execut_db_sql(sql, dbname=self.db_name)
        self.log.info(res)
        self.assertIn('COPY 4', res, '建表失败')

        res = self.sh_primary.execut_db_sql(sql_select, dbname=self.db_name)
        self.log.info(res)
        self.assertEqual(check_msg, res, '文件数据至表失败')

    def tearDown(self):
        self.log.info('-----step4:清理环境; expect:成功-----')
        check_cmd = f'rm -rf {self.copy_path};' \
                    f'if [ -d {self.copy_path} ]; ' \
                    f'then echo "exists"; else echo "not exists"; fi'
        self.log.info(check_cmd)
        res = self.common.get_sh_result(self.user_node, check_cmd)
        sql_cmd = f"drop database if exists {self.db_name};"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd, dbname='postgres')
        self.log.info(sql_res)
        self.assertEqual('not exists', res, '文件删除失败')
        self.assertIn('DROP DATABASE', sql_res, '数据库创建失败')
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
